<template>
  <div class="login-container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="logo_box">
        <img src="../../assets/images/avatar.png" alt="logo" />
      </div>
      <!-- 标题 -->
      <div class="login_title">食验室后台管理系统</div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0px"
        ref="loginRef"
        :model="loginForm"
        :rules="loginRules"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            clearable=true
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            clearable=true
            prefix-icon="iconfont icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" icon="el-icon-lollipop" @click="login"
            >登录</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-circle-plus-outline"
            @click="signin"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单的数据对象
      loginForm: {
        username: '',
        password: '',
      },
      // 表单验证规则对象
      loginRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 3,
            max: 18,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    // 点击重置按钮，重置表单
    // resetLogin() {
    //   // console.log(this);

    // },
    login() {
      this.$refs.loginRef.validate(async (valid) => {
        // console.log(valid);
        const path = this.URL.Login
        console.log(path)
        if (!valid) return
        // {data:res} ES6.0语法中 对象的解构
        const { data: result } = await this.$http
          .post(path, this.loginForm)
          .catch(() => {
            this.$message.error('NETWORK ERROR')
          })
        if (result.code !== 200) return this.$message.error(result.msg)
        // 登录成功
        this.$message.success(result.msg)

        // 1.将成功之后的token保存到客户端的sessionStorage中
        // token令牌
        // window.sessionStorage.setItem('token');
        // 暂时本地持久化 后续更改
        window.localStorage.setItem('admin_token', this.loginForm.username)
        this.$router.push('/home')
      })
    },
    signin() {
      this.$refs.loginRef.validate(async (valid) => {
        const path = this.URL.Signin
        if (!valid) return
        const { data: result } = await this.$http
          .post(path, this.loginForm)
          .catch(() => {
            this.$message.error('NETWORK ERROR')
          })
        if (result.code !== 200) return this.$message.error(result.msg)
        this.$message.success(result.msg)
      })
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #21374b;
  height: 100%;
}

.login_title {
  font-size: 25px;
  font-family: Arial, Helvetica, sans-serif;
  color: orange;
  // letter-spacing: 1px;
  position: absolute;
  top: 12%;
  left: 50%;
  transform: translate(-50%);
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  // 边框阴影
  box-shadow: 0 0 20px whitesmoke;
  // 将login定位到正中央
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  // logo
  .logo_box {
    height: 110px;
    width: 110px;
    // border: 1px solid #eee;
    // border-radius: 50%;
    // padding: 10px;
    // box-shadow: 0 0 10px antiquewhite;

    // 居中
    position: absolute;
    left: 50%;
    transform: translate(-50%, -100%);
    // image
    img {
      // 宽高等于父盒子的宽高
      width: 100%;
      height: 100%;
      // border-radius: 50%;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>